import winston from "winston";
import path from "path";
import fs from "fs";

// Ensure the logs directory exists
const logsDir = path.join(process.cwd(), "logs");
if (!fs.existsSync(logsDir)) {
  fs.mkdirSync(logsDir);
}

// Generate a dynamic filename based on the current date
const logFileName = path.join(
  logsDir,
  `log-${new Date().toISOString().replace(/:/g, "-")}.log`
);

// Create a Winston logger
const logger = winston.createLogger({
  level: "debug", // Default log level
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.printf(({ timestamp, level, message }) => {
      return `${timestamp} [${level.toUpperCase()}]: ${message}`;
    })
  ),
  transports: [
    // Write logs to the console
    // new winston.transports.Console(),
    // Write logs to a file
    new winston.transports.File({ filename: logFileName }),
  ],
});

export default logger;
